/**
 * 修改用户信息
 * 修改性别，出生日期，年龄，简介
 */

const userModel = require('../models/userModel')
const utils = require('../common/utils')

function change(user) {

  user.birthday = utils.changeTime(user.birthday)


  return new Promise((resolve, reject) => {
    userModel.findOne({ user_name: user.name }, (err, suc) => {
      if (err) {
        reject({msg: '用户名查询失败'})
      } else {
        resolve(suc)
      }
    })
  }).then(suc => {
    return new Promise((resolve, reject) => {
      userModel.findOneAndUpdate({ user_name: user.name }, { "$set": { user_age: user.age, user_birthday: user.birthday, user_sex: user.sex, user_desc: user.desc } }).exec((err, res) => {
        if (err) {
          reject(err)
        } else {
          resolve({msg: '更新成功', code: 200})
        }
      })
    })
  })
}


module.exports = {
  change
}